//
//  AddNoteViewController.m
//  SecretFolder
//
//  Created by Deepak Kumar on 11/07/12.
//  Copyright 2012 Frog. All rights reserved.
//

#import "AddNoteViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "Repository.h"
#import "File.h"
#import "AppDelegate_iPad.h"

@implementation AddNoteViewController
{
    NSMutableArray *mOptions;
   File *mFile;
    UITextView *mTextView;
    
}
@synthesize mTableView, mImageView;


#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    mOptions=[NSMutableArray new];
    [mOptions addObject:@"Mark as Favourite"];
    [mOptions addObject:@"Hide"];
    [mOptions addObject:@"AddNote"];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
//	mImageView.layer.cornerRadius = 10.0;
//	mImageView.layer.masksToBounds = YES;
//	mImageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
//	mImageView.layer.borderWidth = 2.0f;

	
}
-(void)setFile:(File *)pFile
{
    mFile=pFile;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}


- (CGSize)contentSizeForViewInPopoverView {
    return CGSizeMake(320.0, 460.0);
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return UIInterfaceOrientationIsLandscape (interfaceOrientation);
}

-(void)Cancel:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

-(void)Save:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}
#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (section == 0) {
         return [mOptions count];
    }
//    else if (section == 1)
//    {
//        return 1;
//    }
    return 1;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
////	if (indexPath.row == 0) {
////		return 60.0f;
////	}
////	else if (indexPath.row == 1){
////		return 160.0f;		
////	}
//    
//    return 60.0f;
//	//return 0.0f;
//}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    if (indexPath.section == 0) {
        if ([mOptions objectAtIndex:[indexPath row]]) {
            cell.textLabel.text = [mOptions objectAtIndex:[indexPath row]];
        }
    }
    else
    {
        cell.textLabel.text = @"Add Note";
    }
    
   cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
      
    NSManagedObjectContext *tContext=[[Repository sharedRepository] context];
    NSFetchRequest *tRequest=[NSFetchRequest fetchRequestWithEntityName:@"File"];
    
    NSPredicate *tPredicate=[NSPredicate predicateWithFormat:@"mName=%@",mFile.mName];
    [tRequest setPredicate:tPredicate];
    NSError *tError;
    NSArray *tFetchObjectsArray=[tContext executeFetchRequest:tRequest error:&tError];
    if (tError) {
        NSLog(@"------Error in fetching the file information %@",tError);
    }
    else
    {
        NSLog(@"---------Fetch request for file successful %@",mFile.mName);
    }

    switch ([indexPath row])
    {
        case 0:
        {
            if ([tFetchObjectsArray count]) {
                mFile=[tFetchObjectsArray objectAtIndex:0];
                mFile.mIsFavorite=[[NSNumber alloc] initWithInt:1];
                [tContext save:&tError];
                if (tError) {
                    NSLog(@"-----error in saving into the database");
                }
            }
            tFetchObjectsArray=[tContext executeFetchRequest:tRequest error:&tError];

            break;
        }
        case 1:
        {
            if ([tFetchObjectsArray count]) {
                mFile=[tFetchObjectsArray objectAtIndex:0];
                mFile.mIsHidden=[[NSNumber alloc] initWithInt:1];
                [tContext save:&tError];
                if (tError) {
                    NSLog(@"-----error in saving into the database");
                }
            }
             break;
        }
           
            
        default:
            break;
    }
[self.mPopoverController dismissPopoverAnimated:YES];
    }
    else
    {
        UIViewController *tController=[[UIViewController alloc] init];
        UIView  *tView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 320, 461)];
        tView.backgroundColor = [UIColor whiteColor];
        mTextView.text = @"Try.....";
        mTextView.textColor = [UIColor blackColor];
        [self presentViewController:tController animated:YES completion:^{
            
        }];
        
    }
}

-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
    return YES;
}


//- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    
//    UILabel *result = nil;
//    if (section == 0){
//        result = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 100)];
//        result.text = @"Settings"; result.backgroundColor = [UIColor clearColor];
//        //[result sizeToFit];
//    }
//    else if(section == 1)
//    {
//        result = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 20, 40)];
//        result.text = @"ADD NOTE"; result.backgroundColor = [UIColor clearColor];
//       // [result sizeToFit];
//    }
//    return result;
//}
#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
	self.mTableView = nil;
	self.mImageView = nil;
}




@end

